define(['amazeui', 'jquery', 'global/url', 'global/constants'],
    function(amazeui, jquery, url, constants) {
        return ['$scope', '$rootScope', 'UrlService', 'ChildrenService', 'AdminService','UtilService',  
        function($scope, $rootScope, UrlService, ChildrenService, AdminService, UtilService) {
  		
  		var params = {
  				'userId': 0,
       	       'accessToken': $scope.accessToken,
       	       'filter': {
       		      'pagesize': 10,
       		      'pno': 1,
       		     'search': ''
       		 }
        } 
        $scope.searchChildren = function (event) {
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.children.pagesize
        	_getChildrenList(params);
        }    

       	$scope.forwardPage = function (event) {
       		var currentList = event.target.offsetParent.className, pno = event.target.text;
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.children.pagesize;
        	params.filter.pno = pno;
        	if (currentList.indexOf('child-pager-container')>0) {
       			_getChildrenList(params);
       		}  
       			 
        }

        $scope.prePage = function (event) {
        	var currentPage = $scope.children.currentPage;
        	if (currentPage === 1) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.children.pagesize;
        	params.filter.pno = currentPage - 1;
        	var currentList = event.target.offsetParent.className;
        	if (currentList.indexOf('child-pager-container')>0) {
       			_getChildrenList(params);
       		}  
        }

        $scope.nextPage = function (event) {
        	var currentPage = $scope.children.currentPage;
        	if (currentPage === $scope.children.totalPagers) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.children.pagesize;
        	params.filter.pno = currentPage + 1;
        	var currentList = event.target.offsetParent.className;
        	if (currentList.indexOf('child-pager-container')>0) {
       			_getChildrenList(params);
       		}  
        }


	    function _getChildrenList (params) {
	    	ChildrenService.listChildren(params).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.children = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          // childService.Child.logout();
	  	          //window.location.href = constants.pages.guide;
	  	        }
            });
	    }
	    UtilService.renderSelect();
	   _getChildrenList(params);



	    $scope.editLevel = function(child) {
	    	var childId = child.userId, childLevel = child.level;
	    	$scope.parameter = childLevel;
	    	$scope.id = childId;
	    	$scope.oper = 'editLevel';
	    }
	     $scope.editCoin = function(child) {
	    	var childId = child.userId, childCoin = child.coin;
	    	$scope.coin = childCoin;
	    	$scope.id = childId;
	    }

	    $scope.editallowSessionCount = function(child) {
	    	var childId = child.userId, childAllowSessionCount = child.allowSessionCount;
	    	$scope.allowSessionCount = childAllowSessionCount;
	    	$scope.id = childId;
	    }

		$scope.rejust = function(parameter) {
		 	var operationVo = {
					'oper': $scope.oper, 
					'id': $scope.id, 
					'data': {
					'level': parameter,									
				}
			};				
			_validateChildrenForm('#edit-childLevel-modal');
			if($('#edit-childLevel-modal').data('amui.validator').isFormValid()) {
				_operateChildren(operationVo);
			}		
		}

		$scope.rejustAllowSessionCount = function(allowSessionCount) {
		 	var operationVo = {
					'oper':'editSessionRestriction', 
					'id': $scope.id, 
					'data': {
					'allowSessionCount': allowSessionCount,	
													
				}
			};				
			_validateChildrenForm('#edit-child-allowSessionCount-modal');
			if($('#edit-child-allowSessionCount-modal').data('amui.validator').isFormValid()) {
				_operateChildren(operationVo);
			}		
		}

		$scope.rejustDeductCoin = function(coin) {
		 	var operationVo = {
					'oper': 'editCoin', 
					'id': $scope.id, 
					'data': {
					'coin': coin,	
													
				}
			};				
			_validateChildrenForm('#edit-childCoin-modal');
			if($('#edit-childCoin-modal').data('amui.validator').isFormValid()) {
				_operateChildren(operationVo);
			}		
		}

		$scope.rejustAddCoin = function(coin) {
		 	var operationVo = {
					'oper': 'addCoin', 
					'id': $scope.id, 
					'data': {
					'coin': coin,	
													
				}
			};				
			_validateChildrenForm('#edit-childCoin-modal');
			if($('#edit-childCoin-modal').data('amui.validator').isFormValid()) {
				_operateChildren(operationVo);
			}		
		}

		$scope.userOper = function(child){
			var oper = child.accountState;
		 	var id = child.userId;
		 	var operationVo = {
					'oper':oper, 
					'id': id, 
					'data': {									
				}
			};
			var param = {
       	       'accessToken': $scope.accessToken,
       	       'operate':JSON.stringify(operationVo)
	       	} ;
			ChildrenService.childAccountStateChange(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
						_refreshChildren();
			  		if (response.jsonResponse.response.meta.code == 200) {

			  		}else if (response.jsonResponse.response.meta.code == 100) {
			  		
			  		};	
			  	}	
			})
		}


		$scope.userClear = function(child){
			var oper = child.accountState;
		 	var id = child.userId;
		 	var operationVo = {
					'oper':oper, 
					'id': id, 
					'data': {									
				}
			};
			var param = {
       	       'accessToken': $scope.accessToken,
       	       'operate': operationVo
	       	} ;
			ChildrenService.childClearDate(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { _refreshChildren();
			  			
			  	}	
			});
		}

		$scope.viewMission = function(child){
			var id = child.userId;
			var param = {
       	       'id': id,
       	       'accessToken': $scope.accessToken,
       	       'filter': {
       		      'pagesize': 8,
       		      'pno': 1,
       		     'search': ''
       		 }
	       	} ;
			ChildrenService.childViewMission(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
			  		  $scope.missions = response.jsonResponse.response.data;
			  		  $scope.id = id;	
			  	}	
			});
		}

		$scope.clearMission = function(mission){
			var userId = mission.userId;
		 	var missionId =  mission.missionId;
		 	var questId = mission.questId;
		 	var param = {
		 		'userId':userId,
		 		'missionId':missionId,
		 		'questId':questId
		 	};
		 	ChildrenService.childMissionQuestClear(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
			  		_refreshviewMission (userId);	
			  	}	
			});	
		}

		$scope.clearAllMission = function(userId){
		 	var apiURL = url.childMissionClearURL() +'?userId='+userId;
		 	var param = {
		 		'userId':userId,
		 	};
		 	ChildrenService.childMissionClear(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
			  		_refreshviewMission (userId);	
			  	}	
			});	
			
		}
		$scope.remove = function(child){
			 if(confirm("Are you sure to delete")){
				var param = {
		 		'userId':child.userId,
		 		};
				ChildrenService.childdelete(param).success(function(response, status) {
					if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
			  			_refreshChildren();	
			  		}	
				});	
 
    		 }
		}

		$scope.findAtem = function(child){
			var param = {
		 		'accessToken': $scope.accessToken,
       	       	'filter': {
       		     	 'pagesize': 8,
       		      	 'pno': 1,
       		    	 'search': ''
       				 }

		 		};
		 	$scope.id = child.userId
	    	ChildrenService.childAddItem(param).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.items = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          // childService.Child.logout();
	  	          //window.location.href = constants.pages.guide;
	  	        }
            });	 
		}

		$scope.info = function(child){
			$scope.userId=child.userId;
			var param = {
		 		'id':$scope.userId,
		 		'accessToken': $scope.accessToken,
       	       	'filter': {
       		     	 'pagesize': 4,
       		      	 'pno': 1,
       		    	 'search': ''
       				 }

		 		};
	    	ChildrenService.findInventory(param).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.inventorys = response.jsonResponse.response.data;
	  		       $scope.totalCoin =  response.jsonResponse.response.meta.code;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          
	  	        }
            });
	    	ChildrenService.findGameSession(param).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.gamesessions = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          
	  	        }
            });
		}

		 function _validateChildrenForm (formId) {
			$(formId).validator({
				onValid: function(validity) {
					$(validity.field).closest('.am-form-group').find('.am-alert').hide();
				},
				onInValid: function(validity) {
					var $field = $(validity.field);
					var $group = $field.closest('.am-form-group');
					var $alert = $group.find('.am-alert');
					var msg = $field.data('validationMessage') || this.getValidationMessage(validity);

					if (!$alert.length) {
						$alert = $('<div class="am-alert am-alert-danger"></div>').hide().
						appendTo($group);
					}
					$alert.html(msg).show();
				}
  			});
		}

		function _operateChildren (child) {
		   var data = {
		   		'accessToken':	$scope.accessToken,
		   		'operate': JSON.stringify(child)
		   };
		    ChildrenService.operateChildren(data).success(function(response, status) {
			  	if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
			  		if (response.jsonResponse.response.meta.code == 200) {
					  	$('.am-close-spin').click();
					  	 _refreshChildren();
			  		}
			  	} else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
			  	         
			  	}
	        });
		}

		function _refreshChildren () {
			params.filter.pagesize = $scope.children.pagesize;
			ChildrenService.listChildren(params).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.children = response.jsonResponse.response.data;

	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          
	  	        }
            });
		}

		function _refreshviewMission (id) {
			var param = {
       	       'id': id,
       	       'accessToken': $scope.accessToken,
       	       'filter': {
       		      'pagesize': 8,
       		      'pno': 1,
       		     'search': ''
       		 }
	       	} ;
			ChildrenService.childViewMission(param).success(function(response, status) {
				if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) { 
			  		  $scope.missions = response.jsonResponse.response.data;
			  		  $scope.id = id;	
			  	}
			});
		}



		}];
	}
)
